package com.rocksea.checkservice;

import android.content.AsyncTaskLoader;
import android.content.Context;
import android.util.Log;

import org.json.JSONArray;
import org.json.JSONObject;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.MarshalBase64;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import java.util.ArrayList;

/**
 * Created by Mrw on 2017/8/19.
 *
 */

public class ServiceTask extends AsyncTaskLoader<ArrayList<ServiceInfo>> {

    public ServiceTask(Context context) {
        super(context);
    }
    @Override
    protected void onStartLoading() {
        super.onStartLoading();
        forceLoad();
    }

    @Override
    public void forceLoad() {
        super.forceLoad();
    }
    @Override
    protected void onStopLoading() {
        super.onStopLoading();
        cancelLoad();
    }
    @Override
    public ArrayList<ServiceInfo> loadInBackground() {
        String serviceNameSpace="http://rsonline.net.cn/";
        String methodName="ServerShortInfoJsonListV3";
        SoapObject request = new SoapObject(serviceNameSpace, methodName);
        SoapSerializationEnvelope envelope=new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.bodyOut=request;
        (new MarshalBase64()).register(envelope);
        String serviceURL="http://update.rsonline.net.cn/rsservice.asmx";
        //Android传输对象
        HttpTransportSE transport=new HttpTransportSE(serviceURL);
        ArrayList<ServiceInfo> mServiceInfos = new ArrayList<>();
        try {
            transport.call(serviceNameSpace + methodName, envelope);
            if (envelope.getResponse() != null) {
                SoapObject object = (SoapObject) envelope.bodyIn;
                String s = object.getProperty(0).toString();
                JSONArray services = new JSONArray(s);
                for (int i = 0; i < services.length(); i++)
                {
                    JSONObject service = (JSONObject) services.get(i);
                    ServiceInfo mServiceInfo = new ServiceInfo();
                    mServiceInfo.setDtuPort(service.getString("DtuPort"));
                    mServiceInfo.setFlag(service.getInt("Flag"));
                    mServiceInfo.setRegionName(service.getString("RegionName"));
                    mServiceInfo.setProvince(service.getString("Province"));
                    mServiceInfo.setServerIp(service.getString("ServerIp"));
                    mServiceInfo.setWebFlag(service.getBoolean("WebFlag"));
                    mServiceInfo.setTransferFlag(service.getInt("TransferFlag"));
                    mServiceInfo.setWebIp(service.getString("WebIp"));
                    mServiceInfo.setWebPort(service.getString("WebPort"));
                    mServiceInfos.add(mServiceInfo);
                }
                DBManger.insert(mServiceInfos);
                return mServiceInfos;
            }
        }catch (Exception e){
            Log.e("loadInBackground: ",e.toString());
        }
        return mServiceInfos;
    }
}
